package com.dimanche.library_base.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dimanche.library_base.R;


/**
 * author : Dimanche
 * date   : 2020/10/22 9:34
 * desc   :错误对话框
 * version: 1.0
 */
public class ErrorDialog extends Dialog {
    private final Context mContext;
    private String msg;
    private TextView tv;


    public ErrorDialog(@NonNull Context context, String msg) {
        super(context);
        mContext = context;
        this.msg = msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        createView();
        setCancelable(false);
    }


    private void createView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setPadding(0, 15, 0, 0);
        linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        ImageView imageView = new ImageView(mContext);
        imageView.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.error));
        linearLayout.addView(imageView);
        tv = new TextView(mContext);
        tv.setText(msg);
        tv.setTextColor(mContext.getResources().getColor(R.color.black));
        tv.setTextSize(40);
        tv.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.addView(tv, tvParams);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        addContentView(linearLayout, params);
    }

    public void setMsg(String msg) {
        this.msg = msg;
        tv.setText(msg);
    }

    @Override
    public void show() {
        super.show();
        setWindowSize();
    }

    /**
     * 设置Dialog窗口的大小
     */
    private void setWindowSize() {
        Window window = getWindow();
        WindowManager manager = window.getWindowManager();
        Display d = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = d.getWidth() / 2;
        params.height = d.getHeight() / 2;
        window.setAttributes(params);
    }


}
